<template>
  <div class="item">
    <div class="name">{{ itemInfo.name }}</div>
    <div class="manager">{{ itemInfo.manager }}</div>
    <el-dropdown @command="commandEvent">
      <div>
        <span>操作</span>
        <i class="el-icon-arrow-down" />
      </div>
      <template #dropdown>
        <!--
          el-dropdown:点击事件
            el-dropdown-item  command="标识"
            el-dropdown @command="(command)=>{command点击项的command的值}"
         -->
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!itemInfo.isTop"
            command="edit"
          >查看部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!itemInfo.isTop"
            command="del"
          >删除部门</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>
<script>
import { companyDepartmentDelete } from '@/api/departments'
export default {
  props: {
    itemInfo: {
      type: Object,
      default: () => ({})
    }
  },
  methods: {
    commandEvent(command) {
      switch (command) {
        case 'add':
          // 触发 toAdd 打开弹框
          // console.log('itemInfo', this.itemInfo)
          this.$bus.$emit('toAdd', true, this.itemInfo.id, 'add')
          break
        case 'del':
          this.$confirm('您确定删除该项吗?', '提示')
            .then(async() => {
              await companyDepartmentDelete(this.itemInfo.id)
              this.$message.success('删除成功')
              // 刷新父级列表数据
              this.$emit('getData')
            })
            .catch(() => {})
          break
        case 'edit':
          // 打开弹框
          console.log(this.itemInfo)
          this.$bus.$emit(
            'toAdd',
            true,
            this.itemInfo.id,
            'edit',
            this.itemInfo
          )
          break
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.item {
  display: flex;
  align-items: center;
  .name {
    flex: 1;
  }
  .manager {
    width: 100px;
  }
}
</style>
